package com.wise.service.cms.content.products;

import cn.hutool.core.collection.CollectionUtil;
import com.wise.entities.content.Product;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.framework.entitiesbasic.IdTimeEntity;
import com.wise.model.content.enums.ProductState;
import com.wise.summer.v1.cms.content.products.ListProducts;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shaoming
 * @date 2019-09-09
 */
@Service
@SummerService
public class ListProductsService implements SummerServiceBean<ListProducts> {

	@Override
	public void sum(ListProducts summer) {

		List<ListProducts.ProductResult> newRecommendProducts = getNewRecommendProductList();
		Map<String, List<ListProducts.ProductResult>> productList4Category = getProductList4Category(summer);

		summer.setSummerResult(ListProducts.Result
				.builder()
				.newRecommendProducts(newRecommendProducts)
				.productList4Category(productList4Category)
				.build());
	}

	/**
	 * 查询新品推荐产品
	 *
	 * @return
	 */
	private List<ListProducts.ProductResult> getNewRecommendProductList() {
		SqlSelect sqlSelect = SqlSelect.selec("*")
				.fromClassName(Product.class)
				.eq(Product._state, ProductState.UP.value())
				.eq(Product._showStatus, true)
				.eq(Product._newRecommend, true)
				.eqLang(Product._lang)
				.order_by(Product._id);

		List<Product> result = DaoList.s(Product.class, sqlSelect);

		// 临时储存更新时间
		result.forEach(temp -> {
			if (temp.getLastUpdateDate() == null){
				temp.setLastUpdateDate(temp.getCreationDate());
			}
		});

		// 按照修改时间排序
		List<Product> sorted = result.stream()
				.sorted(Comparator.comparing(IdTimeEntity::getLastUpdateDate).reversed())
				.collect(Collectors.toList());
		// 复制到输出类
		List<ListProducts.ProductResult> resultList = new ArrayList<>();
		for (Product product : sorted) {
			ListProducts.ProductResult tempResult = new ListProducts.ProductResult();
			BeanUtils.copyProperties(product, tempResult);
			resultList.add(tempResult);
		}

		return resultList;
	}

	/**
	 * @param summer
	 * @return
	 */
	@SummerService(false)
	private Map<String, List<ListProducts.ProductResult>> getProductList4Category(ListProducts summer) {
		if (CollectionUtil.isEmpty(summer.getCategoryIds())){
			return new HashMap<>(1);
		}
		SqlSelect sqlSelect = SqlSelect.selec("*")
				.fromClassName(Product.class)
				.eq(Product._state, ProductState.UP.value())
				.eq(Product._showStatus, true)
				.eqLang(Product._lang)
				.in(CollectionUtil.isNotEmpty(summer.getCategoryIds()), Product._categoryId, summer.getCategoryIds())
				.order_by(Product._id);

		List<ListProducts.ProductResult> productList = DaoList.s(ListProducts.ProductResult.class, sqlSelect);

		return productList.stream()
				.sorted(Comparator.comparingInt(ListProducts.ProductResult::getSort))
				.collect(Collectors.groupingBy(ListProducts.ProductResult::getCategoryId));
	}

}
